package org.idea.qiyu.framework.canal.split.table.handler;

import com.alibaba.otter.canal.protocol.CanalEntry;
import org.idea.qiyu.framework.canal.annotation.CanalListener;
import org.idea.qiyu.framework.canal.handle.AbstractCanalHandler;
import org.idea.qiyu.framework.canal.handle.CanalDataTypeConvertHandler;
import org.idea.qiyu.framework.canal.split.table.po.UserPO;
import org.idea.qiyu.framework.canal.split.table.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author linhao
 * @Date created in 7:52 下午 2021/6/21
 */
@CanalListener(table = "t_user",destination = "example")
@Service
public class UserCanalHandler extends AbstractCanalHandler {

    @Resource
    private UserService userService;

    /**
     * 将数据同步到各个子表当中
     *
     * @param rowChange
     */
    @Override
    public void onUpdate(CanalEntry.RowChange rowChange) {
        super.onUpdate(rowChange);
        for (CanalEntry.RowData rowData : rowChange.getRowDatasList()) {
            UserPO userPO = CanalDataTypeConvertHandler.convertToBean(rowData.getAfterColumnsList(),UserPO.class);
            System.out.println(userPO);
            userService.insertUser(userPO);
        }
    }

    @Override
    public void onDelete(CanalEntry.RowChange rowChange) {
        super.onDelete(rowChange);
        System.out.println(rowChange);
    }

    @Override
    public void onInsert(CanalEntry.RowChange rowChange) {
        super.onInsert(rowChange);
        System.out.println(rowChange);
    }
}
